import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.ReentrantLock;

public class runHis {

    static final int numP = 50;
    private static  int times=1;
    private static final BlockingQueue<Order> queue= new LinkedBlockingQueue<Order>();
    public static void main(String[] args) {


//            final BlockingQueue<Order> queue = new LinkedBlockingQueue<Order>();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < numP; i++) {
                        try {
//                         Thread.sleep(30);
                            Order order = new Order(i);
                            queue.put(order);
                            System.out.println(i + "号已挂...");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("今天的号挂完了，明天再来....");
                }
            }).start();
            ExecutorService pool = Executors.newFixedThreadPool(5);
            Num num = new Num();

            for (int i = 0; i < 5; i++) {

//                ReentrantLock lock = new ReentrantLock(false);
//                lock.lock();
                try {
                    pool.execute(num);
                }finally {
//                    lock.unlock();
                }
            }

            pool.shutdown();

    }
    private static class Num implements Runnable{
        public Thread thread = null;
        @Override
        public void run(){
            //加锁之后 即使线程池中也会按照顺序完成
            synchronized (this) {
                while (times < numP) {
                    try {
                        Order order = (Order) queue.take();
//                        int rand = (int) (Math.random() * 300);
//                        Thread.sleep(rand);
                        System.out.println("医生:" + Thread.currentThread().getId() + ",给" + order.getId() + "号病人看完了...");
                        System.out.println("现在是第" + times + "次看病");
                        setTimes();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    private static int setTimes(){
        return times++;
    }


}
